package com.blog.wu.myblog.util;

import java.io.Serializable;
import java.util.List;

/**
 * @Author wuyanshen
 * @Date 2018-10-10 下午11:18
 * @Discription 在视图、控制器、业务层之间进行数据传输的类
 * 包含查询的结果、以及分页的信息
 */
public class PageBean<T> implements Serializable {

    /**
     * 返回某一项记录的集合
     */
    private List<T> list;

    /**
     * 总记录数
     */
    private int allRow;

    /**
     * 总页数
     */
    private int totalPage;


    /**
     * 当前第几页
     */
    private int currentPage;


    /**
     * 每页记录数
     */
    private String pageSize = Const.PAGE_SIZE;


    //-------start--------页面分页参数
    private int begin;

    private int end;
    //-------end--------页面分页参数


    public int getBegin() {
        return begin;
    }

    public void setBegin(int begin) {
        this.begin = begin;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getAllRow() {
        return allRow;
    }

    public void setAllRow(int allRow) {
        this.allRow = allRow;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public String getPageSize() {
        return pageSize;
    }

    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }

    /*
     * 初始化参数
     *
     * @param
     * @return void
     * @Date 2018/10/31 6:25 PM
     * @Author wuyanshen
     */
    private void initParam() {

        int navSize = Integer.parseInt(Const.NAVE_SIZE);

        if (totalPage <= navSize) {
            begin = 1;
            end = totalPage;
        } else {
            if(navSize%2!=0){
                begin = currentPage - ((navSize-1)/2);
                end = currentPage + ((navSize+1)/2-1);
            }else {
                begin = currentPage - (navSize/2);
                end = currentPage + (navSize/2-1);
            }

            if (begin < 1) {
                begin = 1;
                end = navSize;
            } else if (end > totalPage) {
                begin = totalPage - (navSize-1);
                end = totalPage;
            }
        }
    }

    /**
     * 默认输出当前分页标签
     * ${pageBean}
     */
    @Override
    public String toString() {

        //初始化参数
        initParam();

        StringBuilder sb = new StringBuilder();

        sb.append("<nav>");
        sb.append("<ul class=\"pagination pagenav\">");

        if (currentPage > 1) {
            sb.append("<li>" +
                    "<a onclick=\"findPage('1')\"  href=\"#\">首页</a>" +
                    "</li>" +
                    "<li ><a onclick=\"findPage('" + (currentPage - 1) + "')\" href=\"#\" aria-label=\"Previous\"> <span aria-hidden=\"true\">&laquo;上一页</span> </a></li>"
            );
        } else {
            sb.append("<li class=\"disabled\">" +
                    "<a  href=\"#\">首页</a>" +
                    "</li>" +
                    "<li class=\"disabled\"><a href=\"#\" aria-label=\"Previous\"> <span aria-hidden=\"true\">&laquo;上一页</span> </a></li>"
            );
        }

        for (int i = begin; i <= end; i++) {
            if (currentPage==i) {//点击如果是当前页，里上加属性class="active"
                sb.append(
                        " <li class=\"active\"><a onclick=\"findPage('" + i + "')\" href=\"#\">" + i + "</a></li>"
                );

            } else {
                sb.append(
                        " <li><a onclick=\"findPage('" + i + "')\" href=\"#\">" + i + "</a></li>"
                );
            }

            if (i==end&&end<totalPage) {
                sb.append(
                        " <li><a href=\"#\">...</a></li>"
                );
            }
        }

        if (currentPage < totalPage) {
            sb.append(
                    "<li>" +
                            "<a onclick=\"findPage('" + (currentPage + 1) + "')\" href=\"#\" aria-label=\"Next\"> <span>下一页&raquo;</span></a>" +
                    "</li>" +
                    "<li>" +
                            "<a onclick=\"findPage('" + totalPage + "')\" href=\"#\">尾页</a>" +
                    "</li>"
            );
        } else {
            sb.append(
                    "<li class=\"disabled\">" +
                            "<a  href=\"#\" aria-label=\"Next\"> <span aria-hidden=\"true\">下一页&raquo;</span></a>" +
                    "</li>" +
                    "<li class=\"disabled\">" +
                            "<a  href=\"#\"><span aria-hidden=\"true\">尾页</span></a>" +
                    "</li>"
            );

        }

        sb.append("<li class=\"disabled\">" +
                "<a>共" + allRow + "条,当前" + currentPage + "/" + totalPage + "页</a>" +
                "</li>");

        sb.append("</ul>");
        sb.append("</nav>");

        return sb.toString();
    }
}
